<script setup lang="ts">
import { ArrowLeft } from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'
import { computed, ref } from 'vue'
import type { EnDetailI } from '@/types/park'
import { parkDownloadContractApi, parkGetEnDetailApi } from '@/apis/park'
import { formatRentStatus, formatTagType } from '@/utils/format'
import { id } from 'element-plus/es/locales.mjs'

const route = useRoute()
let enDetail = ref<EnDetailI>({
  id: undefined,
  name: '',
  legalPerson: '', // 企业法人
  registeredAddress: '', // 注册地址
  industryCode: undefined, // 行业编号
  industryName: '', // 行业名称
  businessLicenseUrl: '', // 上传文件url
  businessLicenseName: '', // 上传的营业执照名字
  businessLicenseId: undefined, // 上传文件id
  contact: '', // 企业联系人
  contactNumber: '', // 企业联系方式
  rent: [], // 企业租赁列表数据
})
// 预览地址
const previewURL = 'https://view.officeapps.live.com/op/view.aspx?src='
// 获取企业详情
const getEnDetail = async (id: any) => {
  if (id) enDetail.value = await parkGetEnDetailApi(id)

  // 预处理预览地址
  // PDF：直接浏览器打开
  // 在线预览工具只能浏览 doc 文件，需要拼接前缀预览地址
  enDetail.value.rent.forEach((item) => {
    const url = item.contractUrl
    const extName = url?.slice(url?.lastIndexOf('.'))

    if (extName === '.doc') {
      item.contractUrl = previewURL + url
    }
  })
}
getEnDetail(route.query.id)
// 下载合同
const downloadContract = async (row: any) => {
  const res = await parkDownloadContractApi(row.contractId)
  // 文件流数据 => File对象（父类 Blob）=> 转临时 URL 地址
  const url = window.URL.createObjectURL(res)
  const a = document.createElement('a')
  a.href = url
  a.download = row.contractName || ''
  a.click()
}
</script>

<template>
  <div class="page-container">
    <header class="header-container">
      <el-page-header :icon="ArrowLeft" @back="$router.back()">
        <template #content>
          <span class="text-large font-600 mr-3">查看企业</span>
        </template>
        <template #extra>
          <span>黑马管理员</span>
        </template>
      </el-page-header>
    </header>
    <main class="main-container">
      <!-- 企业信息 -->
      <div class="form-container">
        <div class="title">企业信息</div>
        <el-form>
          <el-form-item label="企业名称:">{{ enDetail.name }}</el-form-item>
          <el-form-item label="法人:">{{ enDetail.legalPerson }}</el-form-item>
          <el-form-item label="注册地址:">{{
            enDetail.registeredAddress
          }}</el-form-item>
          <el-form-item label="所在行业:">{{
            enDetail.industryName
          }}</el-form-item>
          <el-form-item label="营业执照:">
            <img :src="enDetail.businessLicenseUrl" style="width: 200px" />
          </el-form-item>
        </el-form>
      </div>
      <!-- 联系人信息 -->
      <div class="form-container">
        <div class="title">联系人信息</div>
        <el-form>
          <el-form-item label="企业联系人:">{{
            enDetail.contact
          }}</el-form-item>
          <el-form-item label="联系电话:">{{
            enDetail.contactNumber
          }}</el-form-item>
        </el-form>
      </div>
      <!-- 企业租赁记录 -->
      <div class="form-container" style="padding-bottom: 20px">
        <div class="title">租赁记录</div>
        <el-table border :data="enDetail.rent">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="60"
          />
          <el-table-column
            label="租赁楼宇"
            align="center"
            width="120"
            prop="name"
          />
          <el-table-column label="合同状态" align="center" width="100">
            <template #default="{ row }">
              <el-tag :type="formatTagType(row.status)">
                {{ formatRentStatus(row.status) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="租赁起止时间" align="center" width="220">
            <template #default="{ row }">
              {{ row.startTime + ' 至 ' + row.endTime }}
            </template>
          </el-table-column>
          <el-table-column
            label="租赁合同(点击预览)"
            align="center"
            prop="contractName"
          >
            <template #default="{ row }">
              <el-button type="text"
                ><a :href="row.contractUrl">{{
                  row.contractName
                }}</a></el-button
              >
            </template>
          </el-table-column>
          <el-table-column
            label="录入时间"
            align="center"
            prop="createTime"
            width="200"
          />
          <el-table-column label="操作" align="center" width="100">
            <template #default="{ row }">
              <el-button type="text" @click="downloadContract(row)"
                >合同下载</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </main>
  </div>
</template>

<style scoped lang="scss">
.page-container {
  background-color: #f4f6f8;
  height: 100vh;

  .header-container {
    background-color: #fff;
    padding: 20px 20px;
  }

  .main-container {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
        border-bottom: 1px solid rgb(237, 237, 237, 0.9);
      }

      .el-form {
        display: flex;
        flex-wrap: wrap;
        margin-bottom: 20px;
        padding: 30px 160px 24px;

        .el-form-item {
          width: 40%;
          padding-left: 10%;
        }
      }

      .el-table {
        width: 90%;
        margin: 20px 5%;

        a {
          text-decoration: none;
        }
      }
    }
  }
}
</style>
